using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;

using Atomic.Libraries.Mathematics;

namespace Atomic
{
	public static class AtomicEnvironment
	{
		public static double[] ParseArray(string entries)
		{
			List<double> a = new List<double>();
			foreach (string s0 in entries.Split(',', ' ', '\t'))
			{
				if (s0 != "")
				{
					a.Add(double.Parse(s0, CultureInfo.InvariantCulture));
				}
			}

			return a.ToArray();
		}

		public static Vector ParseVector(string entries)
		{
			return new Vector(ParseArray(entries));
		}
		
		public static Matrix ParseMatrix(params string[] rows)
		{
			throw new NotImplementedException();
		}

		public static SpaceVector ParseSpaceVector(string entries)
		{
			return new SpaceVector(ParseArray(entries));
		}

		public static SpaceVector ParseSpaceVector(string entry1, string entry2, string entry3)
		{
			return new SpaceVector(double.Parse(entry1, CultureInfo.InvariantCulture),
				double.Parse(entry2, CultureInfo.InvariantCulture),
				double.Parse(entry3, CultureInfo.InvariantCulture));
		}

		public static SpaceMatrix ParseSpaceMatrix(string row1, string row2, string row3)
		{
			return SpaceMatrix.Transpose(new SpaceMatrix(ParseSpaceVector(row1), ParseSpaceVector(row2), ParseSpaceVector(row3)));
		}

		public static FileInfo ExecutableFile
		{
			get
			{
				string location = Assembly.GetEntryAssembly().Location;
				if (location == "")
				{
					throw new Exception("Unknown location of current executable.");
				}

				return new FileInfo(location);
			}
		}

		public static DirectoryInfo HomeDirectory
		{
			get
			{
				string path = Environment.GetEnvironmentVariable("HOME");
				if (path == null)
				{
					throw new Exception("Undefined HOME directory.");
				}

				return new DirectoryInfo(path);
			}
		}

		public static string HostName
		{
			get
			{
				return Environment.GetEnvironmentVariable("HOSTNAME");
			}
		}
	}
}
